package cn.jbolt.ec.order.handler;

import cn.jbolt.common.model.*;
import cn.jbolt.ec.rush.RushCommodityService;
import cn.jbolt.ec.retailer.RetailerService;
import cn.jbolt.ec.rush.AlreadyService;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;

/**
 * 分销商秒杀
 */
public class TypeO {
    @Inject
    RetailerService retailerService;
    @Inject
    AlreadyService alreadyService;
    @Inject
    RushCommodityService rushCommodityService;



    public JSONObject handle(int client_id, int retailer_id, int commodity_id,
                             int option_id,String consignee, String mobile) {

        /*
        生成新订单
         */
        JSONObject jo = new JSONObject();

        Db.tx(()->{

            BecRetailerOrder becRetailerOrder = new BecRetailerOrder();
            becRetailerOrder.setAddress("分销商秒杀")
                    .setMobile(mobile)
                    .setConsignee(consignee).setStatus(5)
                    .setType(14).setClientId(client_id).setRetailerId(retailer_id);

            long t =System.currentTimeMillis();

            String orderids = "j-" + t+ '-' + client_id + '-' + retailer_id;
            becRetailerOrder.setOrderId(orderids).setOrderTime(t);
            if (!becRetailerOrder.save()) ; // 订单生成失败

            //  是否已经参加分销商秒杀
            Kv cond = Kv.by("type", 14).set("member_id", client_id);
            BecRushAlready bb =  alreadyService.findFirst(cond);
            if (bb!=null) {
                jo.put("ok", 1);
                return false;
            }


            cond = Kv.by("retailer_id", retailer_id).set("commodity_id", commodity_id)
                    .set("option_id", option_id);
            BecRushstoreCommodity becRushstoreCommodity = rushCommodityService.findFirst(cond);
            BecMember m = new BecMember().dao().findByIdLoadColumns(client_id, "id,spreader_credit");

            if (becRushstoreCommodity == null) {
                jo.put("ok", 2);
                return false;

            }

            if (becRushstoreCommodity.getCreditStock() < 1) {
                jo.put("ok", 3);
                return false;

            }
            if (m == null || m.getSpreaderCredit() < becRushstoreCommodity.getCreditPrice()) {
                jo.put("ok", 4);
                return false;

            }
            m.setSpreaderCredit(m.getSpreaderCredit()-becRushstoreCommodity.getCreditPrice());
            becRetailerOrder.setAmountPaid((long)becRushstoreCommodity.getCreditPrice())
                    .setAmountPaid((long)becRushstoreCommodity.getCreditPrice());
            if(!m.update()){
                jo.put("ok", 9);
                return false;

            }
            // 积分已支付
            becRetailerOrder.setStatus(9);

            becRushstoreCommodity.setCreditStock(becRushstoreCommodity.getCreditStock() - 1);
            if (!becRushstoreCommodity.update()) {
                jo.put("ok", 5);
                return false;
            }


            // 重用charge_id放rushstorecommodityid
            becRetailerOrder.setChargeId(becRushstoreCommodity.getId());
            if (!becRetailerOrder.update()) {
                jo.put("ok", 6);
                return false;


            }
            BecRushAlready becRushAlready = new BecRushAlready();
            boolean b = becRushAlready.setMemberId(client_id)
                    .setType(14).save();
            if (!b) {
                jo.put("ok", 7);
                return false;


            }



            jo.put("ok", 0);
            jo.put("id", becRetailerOrder.getId());

            jo.put("order",becRetailerOrder);
            return true;


        });




        return jo;


    }

}
